ALMaSS  1.0
The Animal, Landscape and Man Simulation System
Weather Class Reference

#include <weather.h>

Public Member Functions

 Weather (const char *a_weatherdatafile)
 
 ~Weather (void)
 
double GetDDDegs (long a_date)
 
double GetGlobalRadiation (long a_date)
 
double GetGlobalRadiation (void)
 
double GetTemp (long a_date)
 Get the temperature on a particular date. More...
 
double GetTemp (void)
 Get the temperature today. More...
 
double GetHumidity (void)
 Get the humidity score today. More...
 
double GetMeanTemp (long a_date, unsigned int a_period)
 
double GetRain (long a_date)
 
double GetRain (void)
 
double GetWind (long a_date)
 
double GetWind (void)
 
int GetWindDirection (void)
 
bool GetSnow (long a_date)
 
bool GetSnow (void)
 
double GetSnowDepth (void)
 Get the current snow depth. More...
 
bool Raining (void)
 
double GetRainPeriod (long a_date, unsigned int a_period)
 
double GetWindPeriod (long a_date, unsigned int a_period)
 
double GetTempPeriod (long a_date, unsigned int a_period)
 
void Tick (void)
 

Private Attributes

vector< double > m_rain
 
vector< double > m_wind
 
vector< double > m_winddir
 
vector< double > m_temp
 
double m_temptoday
 
double m_raintoday
 
double m_windtoday
 
int m_winddirtoday
 
bool m_snowtoday
 
bool m_rainingtoday
 
double m_insolation
 
double m_humiditytoday
 
bool m_wind_valid
 
bool m_winddir_valid
 
long m_datemodulus
 
double m_snowdepth
 The snow depth in cm. More...
 

Constructor & Destructor Documentation

◆ Weather()

Weather::Weather ( const char *  a_weatherdatafile)
168 {
169  int NoDays, Day, Month, Year, FirstYear=0, LastYear=0;
170  double Temp, Rain, Wind;
171 
172  ifstream inFile(a_weatherdatafile);
173  if (!inFile) {
174  g_msg->Warn(WARN_FILE, "Weather::Weather(): Unable to open file",
175  a_weatherdatafile );
176  exit(1);
177  }
178 
179  string buf;
180  std::getline(inFile,buf);
181  NoDays=std::stoi(buf);
182 
183 
184  m_rain.resize( NoDays );
185  m_wind.resize( NoDays );
186  m_temp.resize( NoDays );
187 
188  bool storing = false; // Whether we are storing weather data.
189  unsigned int index = 0;
190 
191  g_date->SetLastYear( 0 );
192 
193 
194  for ( int i=0; i<NoDays; i++) {
195  inFile >> Year >> Month >> Day >> Temp >> Wind >> Rain;
196 
197  if ( Month == 2 && Day == 29 ) {
198  // Skip leap days.
199  continue;
200  }
201 
202  if ( Month == 1 && Day == 1 && !storing &&
203  (
206  )
207  ) {
208  // Commence storing of data from Jan. 1st of the first
209  // year requested.
210  storing = true;
211  g_date->SetFirstYear( Year );
212  FirstYear = Year;
213  g_date->Reset();
214  }
215 
216 
217  if ( storing ) {
218  //if (Temp > 0.0 )
219  // DDegs += Temp;
220  m_rain[ index ] = Rain;
221  m_wind[ index ] = Wind;
222  m_temp[ index ] = Temp;
223  index++;
224  }
225 
226  //cout << "i: " << i << " index: " << index << " Month: " << Month << " Year: " << Year <<"\n";
227 
228  if ( (Month == 12) && (Day == 31) && (storing) ) {
229  cout << "LastYear: " << Year << "\n";
230  // Found yet another full year worth of weather data.
231  g_date->SetLastYear( Year );
232  LastYear = Year;
233  }
234  }
235 
236  // Did we find at least one full year worth of data?
237  if ( g_date->GetLastYear() == 0 ) {
238  // Nope...
239  g_msg->Warn(WARN_FILE, "Weather::Weather(): Weather data file did",
240  "not contain at least one year of data!" );
241  exit(1);
242  }
243  //fclose( ifile );
244  m_datemodulus = (LastYear - FirstYear + 1)*365;
245  m_snowdepth = 0;
246  Tick();
247 }
class Calendar * g_date
Definition: calendar.cpp:38
void SetFirstYear(int a_year)
Definition: calendar.h:76
int GetLastYear(void)
Definition: calendar.h:66
void SetLastYear(int a_year)
Definition: calendar.h:77
void Reset(void)
Definition: calendar.cpp:40
int value(void)
Definition: configurator.h:98
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
vector< double > m_temp
Definition: weather.h:390
long m_datemodulus
Definition: weather.h:404
vector< double > m_rain
Definition: weather.h:387
double m_snowdepth
The snow depth in cm.
Definition: weather.h:407
vector< double > m_wind
Definition: weather.h:388
void Tick(void)
Definition: weather.cpp:58
class MapErrorMsg * g_msg
This pointer provides access the to the internal ALMaSS error message system.
Definition: maperrormsg.cpp:41
@ WARN_FILE
Definition: maperrormsg.h:37
static CfgInt l_weather_starting_year("WEATHER_STARTING_YEAR", CFG_CUSTOM, 0)

References g_date, g_msg, Calendar::GetLastYear(), l_weather_starting_year, m_datemodulus, m_rain, m_snowdepth, m_temp, m_wind, Calendar::Reset(), Calendar::SetFirstYear(), Calendar::SetLastYear(), Tick(), CfgInt::value(), MapErrorMsg::Warn(), and WARN_FILE.

◆ ~Weather()

Weather::~Weather ( void  )
250 {
251 }

Member Function Documentation

◆ GetDDDegs()

double Weather::GetDDDegs ( long  a_date)
156 {
157  double temp = m_temp[ a_date%m_datemodulus ];
158  if ( temp < 0.0 ) {
159  temp = 0.0;
160  }
161 
162  return temp;
163 }

References m_datemodulus, and m_temp.

Referenced by VegElement::DoDevelopment(), UnsprayedFieldMargin::DoDevelopment(), and VegElement::SetGrowthPhase().

◆ GetGlobalRadiation() [1/2]

double Weather::GetGlobalRadiation ( long  a_date)
inline
414 { return (double)c_insolation[a_date % m_datemodulus]; }
const double c_insolation[365]
Definition: weather.h:16

References c_insolation.

Referenced by Landscape::SupplyGlobalRadiation().

◆ GetGlobalRadiation() [2/2]

double Weather::GetGlobalRadiation ( void  )
inline
415 { return m_insolation; }
double m_insolation
Definition: weather.h:398

◆ GetHumidity()

double Weather::GetHumidity ( void  )
inline

Get the humidity score today.

421 { return m_humiditytoday; }
double m_humiditytoday
Definition: weather.h:399

Referenced by Landscape::SupplyHumidity().

◆ GetMeanTemp()

double Weather::GetMeanTemp ( long  a_date,
unsigned int  a_period 
)
121 {
122  double sum = 0.0;
123 
124  for ( unsigned int i=0; i<a_period; i++ )
125  sum += GetTemp( a_date - i );
126 
127  return sum/(double)a_period;
128 }
double GetTemp(void)
Get the temperature today.
Definition: weather.h:419

References GetTemp().

Referenced by Landscape::SupplyMeanTemp().

◆ GetRain() [1/2]

double Weather::GetRain ( long  a_date)
inline

◆ GetRain() [2/2]

double Weather::GetRain ( void  )
inline
424 { return m_raintoday; }
double m_raintoday
Definition: weather.h:393

Referenced by GetRainPeriod(), and Tick().

◆ GetRainPeriod()

double Weather::GetRainPeriod ( long  a_date,
unsigned int  a_period 
)
254 {
255  double sum = 0.0;
256 
257  for ( unsigned int i=0; i<a_period; i++ )
258  sum += GetRain( a_date - i );
259  return sum;
260 }
double GetRain(void)
Definition: weather.h:424

References GetRain().

Referenced by Farm::BulbHarvest(), Farm::BurnStrawStubble(), Farm::CutToHay(), Farm::Harvest(), OptimisingFarm::Harvest(), Farm::HarvestLong(), Farm::HayBailing(), Farm::HayTurning(), Farm::RowCultivation(), Farm::StrawChopping(), Farm::Strigling(), Farm::StriglingSow(), Farm::StubbleHarrowing(), and Landscape::SupplyRainPeriod().

◆ GetSnow() [1/2]

bool Weather::GetSnow ( long  a_date)
133 {
134  if ( a_date == g_date->Date() ) {
135  return m_snowtoday;
136  }
137 
138  int weatherday = a_date % m_datemodulus;
139  bool snow = false;
140 
141  if ( m_temp[ weatherday ] < 0 &&
142  m_rain[ weatherday ] > 1 &&
143  random(100) < 50 ) {
144  snow = true;
145  }
146 
147  // if ( ((dayinyear<90) || (dayinyear>330)) && (rand()%4==1) )
148  // snow = true;
149 
150  return snow;
151 }
int random(int a_range)
Definition: ALMaSS_CmdLine.cpp:142
long Date(void)
Definition: calendar.h:57
bool m_snowtoday
Definition: weather.h:396

References Calendar::Date(), g_date, m_datemodulus, m_rain, m_snowtoday, m_temp, and random().

Referenced by Landscape::SupplySnowcover().

◆ GetSnow() [2/2]

bool Weather::GetSnow ( void  )
inline
429 { return m_snowtoday; }

◆ GetSnowDepth()

double Weather::GetSnowDepth ( void  )
inline

Get the current snow depth.

431 { return m_snowdepth; }

Referenced by Landscape::SupplySnowDepth().

◆ GetTemp() [1/2]

double Weather::GetTemp ( long  a_date)
inline

◆ GetTemp() [2/2]

double Weather::GetTemp ( void  )
inline

Get the temperature today.

419 { return m_temptoday; }
double m_temptoday
Definition: weather.h:392

Referenced by GetMeanTemp(), and GetTempPeriod().

◆ GetTempPeriod()

double Weather::GetTempPeriod ( long  a_date,
unsigned int  a_period 
)

Sums the temperature for the period from a_date back a_period days.

Parameters
[in]a_datethe day to start summing degrees
[in]a_periodthe number of days perĂ­od to sum
Returns
The sum of day degrees
273 {
280  double sum = 0.0;
281 
282  for ( unsigned int i=0; i<a_period; i++ )
283  sum += GetTemp( a_date - i );
284 
285  return sum;
286 }

References GetTemp().

Referenced by Skylark_Population_Manager::DoFirst(), Landscape::SupplyTempPeriod(), and Tick().

◆ GetWind() [1/2]

◆ GetWind() [2/2]

double Weather::GetWind ( void  )
inline
426 { return m_windtoday; }
double m_windtoday
Definition: weather.h:394

Referenced by GetWindPeriod().

◆ GetWindDirection()

int Weather::GetWindDirection ( void  )
inline
427 { return m_winddirtoday; }
int m_winddirtoday
Definition: weather.h:395

Referenced by Landscape::SupplyWindDirection().

◆ GetWindPeriod()

double Weather::GetWindPeriod ( long  a_date,
unsigned int  a_period 
)
263 {
264  double sum = 0.0;
265 
266  for ( unsigned int i=0; i<a_period; i++ )
267  sum += GetWind( a_date - i );
268 
269  return sum;
270 }
double GetWind(void)
Definition: weather.h:426

References GetWind().

Referenced by Skylark_Population_Manager::DoFirst(), and Landscape::SupplyWindPeriod().

◆ Raining()

bool Weather::Raining ( void  )
inline

◆ Tick()

void Weather::Tick ( void  )

The complicated calculation of weatherday is due to the potential to run off either end of the rain data, especially since this is called on Day0

Humidity calculation. This is based on an assumption that humidity is relative to rainfall in the days before. The humidity score is the mean of rainfall over the last 5 days assuming 50% loss each day. Therefore rainfall of 10,0,0,2,0 mm in the previous 5 days would give a humidity of 0.625+0+0+1+0 = 1.625/5 = 0.325 But we also need to calculate temperature into this. Lets assume <10 degrees has no effect, 10-20 and this figure is halved, 20-40 and it is half again.

datemodulus is added to prevent negative overun on the rain or temperature data array, important since this is called on Day0

Wind directions is based on probabilities. Can also use the WindDirections array which allows montly probabilities

58  {
60  int weatherday = (g_date->Date() + m_datemodulus) % m_datemodulus;
61  double snowtempthreshold = -1.0;
62  if (m_snowdepth > 0.0 && m_temp[ weatherday ] < snowtempthreshold) {
63  m_snowdepth -= 0.1; // We decay the snow depth by 0.1 cm per day when temperatures are sub zero
64  }
65  else if (m_snowdepth > 0.0) // If temperatures are above 0.0, we decay snow 1 cm per degree C
66  {
67  m_snowdepth -= m_temp[ weatherday ];
68  if (m_snowdepth < 0.0) m_snowdepth = 0.0;
69  }
70 
71  if (m_temp[ weatherday ] < snowtempthreshold && m_rain[ weatherday ] > 0.0) {
72  m_snowdepth += m_rain[ weatherday ]; // rain is in mm and snow in cm, so we get the conversion for free.
73  }
74  if (m_temp[ weatherday ] > snowtempthreshold && m_rain[ weatherday ] > 0.0) {
75  m_snowdepth -= m_rain[ weatherday ]; // rain is in mm and snow in cm, so we get the conversion for free.
76  if (m_snowdepth < 0.0) m_snowdepth = 0.0;
77  }
78  if (m_snowdepth > 0.0) {
79  m_snowtoday = true;
80  }
81  else {
82  m_snowtoday = false;
83  }
84 
86  m_temptoday = m_temp[ weatherday ];
87  m_raintoday = m_rain[ weatherday ];
88  m_windtoday = m_wind[ weatherday ];
89 
90  if (m_raintoday > 0.001)
91  m_rainingtoday = true;
92  else
93  m_rainingtoday = false;
100  double rainscale = 0;
101  for (int i = 0; i < 5; i++) {
103  rainscale += GetRain( (weatherday + m_datemodulus) - (i + 1) ) * pow( 0.5, i );
104  }
105  double temp = GetTempPeriod( weatherday + m_datemodulus - 1, 5 ) / 5.0;
106  if (temp > 10.0) rainscale *= 0.5;
107  if (temp > 20.0) rainscale *= 0.5;
108  if (temp > 30.0) rainscale *= 0.5;
109  m_humiditytoday = rainscale;
111  double chance = g_rand_uni();
112  if (chance < 0.1) m_winddirtoday = 2; // South
113  else if (chance < 0.3) m_winddirtoday = 1; // East
114  else if (chance < 0.5) m_winddirtoday = 0; // North
115  else m_winddirtoday = 3; // West
116 }
int DayInYear(void)
Definition: calendar.h:58
double GetTempPeriod(long a_date, unsigned int a_period)
Definition: weather.cpp:272
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni

References c_insolation, Calendar::Date(), Calendar::DayInYear(), g_date, g_rand_uni, GetRain(), GetTempPeriod(), m_datemodulus, m_humiditytoday, m_insolation, m_rain, m_rainingtoday, m_raintoday, m_snowdepth, m_snowtoday, m_temp, m_temptoday, m_wind, m_winddirtoday, and m_windtoday.

Referenced by Landscape::Tick(), and Weather().

Member Data Documentation

◆ m_datemodulus

long Weather::m_datemodulus
private

Referenced by GetDDDegs(), GetSnow(), Tick(), and Weather().

◆ m_humiditytoday

double Weather::m_humiditytoday
private

Referenced by Tick().

◆ m_insolation

double Weather::m_insolation
private

Referenced by Tick().

◆ m_rain

vector<double> Weather::m_rain
private

Referenced by GetSnow(), Tick(), and Weather().

◆ m_rainingtoday

bool Weather::m_rainingtoday
private

Referenced by Tick().

◆ m_raintoday

double Weather::m_raintoday
private

Referenced by Tick().

◆ m_snowdepth

double Weather::m_snowdepth
private

The snow depth in cm.

Referenced by Tick(), and Weather().

◆ m_snowtoday

bool Weather::m_snowtoday
private

Referenced by GetSnow(), and Tick().

◆ m_temp

vector<double> Weather::m_temp
private

Referenced by GetDDDegs(), GetSnow(), Tick(), and Weather().

◆ m_temptoday

double Weather::m_temptoday
private

Referenced by Tick().

◆ m_wind

vector<double> Weather::m_wind
private

Referenced by Tick(), and Weather().

◆ m_wind_valid

bool Weather::m_wind_valid
private

◆ m_winddir

vector<double> Weather::m_winddir
private

◆ m_winddir_valid

bool Weather::m_winddir_valid
private

◆ m_winddirtoday

int Weather::m_winddirtoday
private

Referenced by Tick().

◆ m_windtoday

double Weather::m_windtoday
private

Referenced by Tick().


The documentation for this class was generated from the following files: